﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace FDSimpleModelGenerator.Common.Mvc.Filters
{
    /// <summary>
    /// 检测是否无需登录、注册
    /// </summary>
    public class LoginRepeatCheckAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 验证已登录则跳转到
        /// </summary>
        public string RedirectTo { get; set; } = "/";

        public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var logined = context.HttpContext.User.Identity.IsAuthenticated;
            if (logined)
            {
                context.Result = new RedirectResult(RedirectTo);
                return Task.CompletedTask;
            }
            return base.OnActionExecutionAsync(context, next);
        }
    }
}
